﻿using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Shard;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RavenDB_FailTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Sample> samples;

            //samples = CanIgnore();
            samples = CanIgnoreParallel();
            //samples = CanIgnore_Lazy()

            foreach (Sample s in samples) Console.WriteLine(s.Name);
            Console.WriteLine("Done.");
            Console.ReadKey();
        }

        public static List<Sample> CanIgnore()
        {
            var shardingStrategy = new ShardStrategy(new Dictionary<string, IDocumentStore>
            {
                {"one", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "DDBS"}},
                {"two", new DocumentStore {Url = "http://localhost:8082", DefaultDatabase = "DDBS"}},
            });
            shardingStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => request.Query != null;
            using (var docStore = new ShardedDocumentStore(shardingStrategy).Initialize())
            {
                using (var session = docStore.OpenSession())
                {
                    return session.Query<Sample>()
                        .ToList();
                }
            }
        }

        public static List<Sample> CanIgnoreParallel()
        {
            //var shardingStrategy = new ShardStrategy(new Dictionary<string, IFilesStore>
            //{
            //    {"FS_one", new FilesStore() { Url = "http://localhost:8080", DefaultFileSystem = "MyFS" }},
            //    {"FS_two", new FilesStore() { Url = "http://localhost:8081", DefaultFileSystem = "MyFS" }},
            //})
            var shardingStrategy = new ShardStrategy(new Dictionary<string, IDocumentStore>
            {
                {"one", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "DDBS"}},
                {"two", new DocumentStore {Url = "http://localhost:8082", DefaultDatabase = "DDBS"}},
            })
            {
                ShardAccessStrategy = new ParallelShardAccessStrategy()
            };
            shardingStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => request.Query != null;
            shardingStrategy.ShardAccessStrategy.OnAsyncError += (commands, request, exception) => request.Query != null;
            using (var docStore = new ShardedDocumentStore(shardingStrategy).Initialize())
            {
                using (var session = docStore.OpenSession())
                {
                    return session.Query<Sample>()
                        .ToList();
                }
            }
        }

        public static void CanIgnore_Lazy()
        {
            var shardingStrategy = new ShardStrategy(new Dictionary<string, IDocumentStore>
            {
                {"one", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "DDBS"}},
                {"two", new DocumentStore {Url = "http://localhost:8082", DefaultDatabase = "DDBS"}},
            });
            shardingStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => true;
            shardingStrategy.ShardAccessStrategy.OnAsyncError += (commands, request, exception) => request.Query != null;
            using (var docStore = new ShardedDocumentStore(shardingStrategy).Initialize())
            {
                using (var session = docStore.OpenSession())
                {
                    var lazily = session.Query<Sample>().Lazily();
                    GC.KeepAlive(lazily.Value);
                }
            }
        }
    }
}
